package com.songcha.library_common.ui.view

import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Path
import android.graphics.RectF
import android.graphics.drawable.GradientDrawable
import android.util.AttributeSet
import android.view.View
import android.widget.FrameLayout

import com.songcha.library_common.R
import com.songcha.library_common.util.AlgorithmUtil


class CustomContainerLayout
@JvmOverloads
constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyle: Int = 0,
    defRes: Int = 0
) : FrameLayout(context,attrs,defStyle,defRes){
    private var mIsAdd=false
    private var mOriginChild:View?=null
    //private lateinit var mContainer:FrameLayout
    private var mRadii=FloatArray(8)
    private var mClipPath:Path?=null
    private var mBorderWidth=0f
    private var mBorderColor=Color.BLACK
    private var mRadius=FloatArray(4)
    //private var mShadowWidth=0f
    //private var mShadowPaint:Paint?=null
    //private var mShadowColor=Color.parseColor("#7f000000")
    //private var mShadowRect:RectF?=null
    //shodow will draw on background 
    private var mBackgroundColor=Color.TRANSPARENT
    private var mStartColor=Color.TRANSPARENT//gradient start color
    private var mEndColor=Color.TRANSPARENT//gradient end color
    private var mGradientOrientation=1
    private var mOriginPaddingLeft=0
    private var mOriginPaddingTop=0
    private var mOriginPaddingRight=0
    private var mOriginPaddingBottom=0


    init {
        initView(context,attrs)
    }

    private fun initView(context: Context, attrs: AttributeSet?){
        setWillNotDraw(false)

        //mContainer=LayoutInflater.from(context).inflate(R.layout.common_view_test,this,false) as FrameLayout
        //mContainer.setBackgroundColor(Color.TRANSPARENT)
        setBackgroundColor(Color.TRANSPARENT)

        var radius=0f
        var lr=0f
        var tr=0f
        var rr=0f
        var br=0f

        if(attrs!=null){
            val ta=context.obtainStyledAttributes(attrs, R.styleable.CustomContainerLayout)
            mBackgroundColor = ta.getColor(R.styleable.CustomContainerLayout_container_background_color,Color.TRANSPARENT)
            mStartColor = ta.getColor(R.styleable.CustomContainerLayout_container_start_color,Color.TRANSPARENT)
            mEndColor = ta.getColor(R.styleable.CustomContainerLayout_container_end_color,Color.TRANSPARENT)
            mGradientOrientation = ta.getInt(R.styleable.CustomContainerLayout_container_gradient_orientation,1)
            mBorderWidth = ta.getDimension(R.styleable.CustomContainerLayout_container_border_width,0f)
            mBorderColor = ta.getColor(R.styleable.CustomContainerLayout_container_border_color,Color.TRANSPARENT)
            radius = ta.getDimension(R.styleable.CustomContainerLayout_container_radius,0f)
            lr=ta.getDimension(R.styleable.CustomContainerLayout_container_left_radius,0f)
            tr=ta.getDimension(R.styleable.CustomContainerLayout_container_top_radius,0f)
            rr=ta.getDimension(R.styleable.CustomContainerLayout_container_right_radius,0f)
            br=ta.getDimension(R.styleable.CustomContainerLayout_container_bottom_radius,0f)
            ta.recycle()
        }

        if(radius>0){
            mRadius[0]=radius
            mRadius[1]=radius
            mRadius[2]=radius
            mRadius[3]=radius
        }else{
            mRadius[0]=lr
            mRadius[1]=tr
            mRadius[2]=rr
            mRadius[3]=br
        }

        //initPadding()

        initDrawable()

        //initShadow()

    }

    fun setBorder(borderColor:Int,borderWidth:Float){
        if(borderColor==mBorderColor && borderWidth==mBorderWidth) return
        mBorderColor=borderColor
        mBorderWidth=borderWidth
        initDrawable()
    }

    fun setRadius(radius:Float){
        setRadius(floatArrayOf(radius))
    }

    fun setRadius(radius:FloatArray){
        val r=AlgorithmUtil.formatStyleFloatArray(radius)
        mRadius=r
        initDrawable()
    }

    fun setContainerBackgroundColor(color: Int){
        mBackgroundColor=color
        initDrawable()
    }

    fun setStartColor(color:Int){
        mStartColor=color
        initDrawable()
    }

    fun setEndColor(color: Int){
        mEndColor=color
        initDrawable()
    }

    /*private fun initPadding(){
      mOriginPaddingLeft=paddingLeft
       mOriginPaddingTop=paddingTop
       mOriginPaddingRight=paddingRight
       mOriginPaddingBottom=paddingBottom

        //setPadding(mShadowWidth.toInt(),mShadowWidth.toInt(),mShadowWidth.toInt(),mShadowWidth.toInt())
        //mContainer.setPadding(mOriginPaddingLeft,mOriginPaddingTop,mOriginPaddingRight,mOriginPaddingBottom)
    }*/

    private fun initDrawable(){
        var gradientOrientation=GradientDrawable.Orientation.LEFT_RIGHT
        when(mGradientOrientation){
            1->gradientOrientation=GradientDrawable.Orientation.LEFT_RIGHT
            2->gradientOrientation=GradientDrawable.Orientation.RIGHT_LEFT
            3->gradientOrientation=GradientDrawable.Orientation.TOP_BOTTOM
            4->gradientOrientation=GradientDrawable.Orientation.BOTTOM_TOP
        }

        val drawable=GradientDrawable()
        if(mStartColor!=Color.TRANSPARENT && mEndColor!=Color.TRANSPARENT){
            drawable.orientation=gradientOrientation
            drawable.colors=intArrayOf(mStartColor,mEndColor)
            drawable.gradientType=GradientDrawable.LINEAR_GRADIENT
        }else{
            drawable.setColor(mBackgroundColor)
        }

        mRadii=AlgorithmUtil.formatRadiiFloatArray(mRadius)

        drawable.setStroke(mBorderWidth.toInt(),mBorderColor)
        //mRadii= floatArrayOf(mRadius,mRadius,mRadius,mRadius,mRadius,mRadius,mRadius,mRadius)
        //mRadii=mRadius
        drawable.cornerRadii=mRadii
        background=drawable
        //mContainer.background=drawable
    }

    /*private fun initShadow(){

        if(mShadowWidth>0){
            setLayerType(LAYER_TYPE_SOFTWARE, null)
            mShadowPaint=Paint()
            mShadowPaint!!.color=mShadowColor
            mShadowPaint!!.isAntiAlias=true
            //mShadowPaint!!.setShadowLayer(mShadowWidth,0f,0f,mShadowColor)
            mShadowPaint!!.maskFilter= BlurMaskFilter(mShadowWidth, BlurMaskFilter.Blur.NORMAL)
        }
    }*/

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
    }


    override fun onLayout(changed: Boolean, l: Int, t: Int, r: Int, b: Int) {
        super.onLayout(changed, left, top, right, bottom)

        if(mRadius[0]!=0f || mRadius[1]!=0f || mRadius[2]!=0f || mRadius[3]!=0f){
            mClipPath= Path()
            val rect= RectF(0f,0f,width.toFloat(),height.toFloat())
            mClipPath!!.addRoundRect(rect,mRadii,Path.Direction.CW)
        }

    }

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)

        if(canvas==null) return

        mClipPath?.let {
            canvas.clipPath(it)
        }

    }

}